package com.ruoyi.bookSys.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.bookSys.domain.BookInfo;
import com.ruoyi.bookSys.mapper.BookCategoryMapper;
import com.ruoyi.bookSys.domain.BookCategory;
import com.ruoyi.bookSys.service.IBookCategoryService;

/**
 * 图书分类Service业务层处理
 * 
 * @author liugang
 * @date 2025-10-14
 */
@Service
public class BookCategoryServiceImpl implements IBookCategoryService 
{
    @Autowired
    private BookCategoryMapper bookCategoryMapper;

    /**
     * 查询图书分类
     * 
     * @param categoryId 图书分类主键
     * @return 图书分类
     */
    @Override
    public BookCategory selectBookCategoryByCategoryId(Long categoryId)
    {
        return bookCategoryMapper.selectBookCategoryByCategoryId(categoryId);
    }

    /**
     * 查询图书分类列表
     * 
     * @param bookCategory 图书分类
     * @return 图书分类
     */
    @Override
    public List<BookCategory> selectBookCategoryList(BookCategory bookCategory)
    {
        return bookCategoryMapper.selectBookCategoryList(bookCategory);
    }

    /**
     * 新增图书分类
     * 
     * @param bookCategory 图书分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookCategory(BookCategory bookCategory)
    {
        bookCategory.setCreateTime(DateUtils.getNowDate());
        int rows = bookCategoryMapper.insertBookCategory(bookCategory);
        insertBookInfo(bookCategory);
        return rows;
    }

    /**
     * 修改图书分类
     * 
     * @param bookCategory 图书分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookCategory(BookCategory bookCategory)
    {
        bookCategory.setUpdateTime(DateUtils.getNowDate());
        bookCategoryMapper.deleteBookInfoByCategoryId(bookCategory.getCategoryId());
        insertBookInfo(bookCategory);
        return bookCategoryMapper.updateBookCategory(bookCategory);
    }

    /**
     * 批量删除图书分类
     * 
     * @param categoryIds 需要删除的图书分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookCategoryByCategoryIds(Long[] categoryIds)
    {
        bookCategoryMapper.deleteBookInfoByCategoryIds(categoryIds);
        return bookCategoryMapper.deleteBookCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除图书分类信息
     * 
     * @param categoryId 图书分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookCategoryByCategoryId(Long categoryId)
    {
        bookCategoryMapper.deleteBookInfoByCategoryId(categoryId);
        return bookCategoryMapper.deleteBookCategoryByCategoryId(categoryId);
    }

    /**
     * 新增图书信息信息
     * 
     * @param bookCategory 图书分类对象
     */
    public void insertBookInfo(BookCategory bookCategory)
    {
        List<BookInfo> bookInfoList = bookCategory.getBookInfoList();
        Long categoryId = bookCategory.getCategoryId();
        if (StringUtils.isNotNull(bookInfoList))
        {
            List<BookInfo> list = new ArrayList<BookInfo>();
            for (BookInfo bookInfo : bookInfoList)
            {
                bookInfo.setCategoryId(categoryId);
                list.add(bookInfo);
            }
            if (list.size() > 0)
            {
                bookCategoryMapper.batchBookInfo(list);
            }
        }
    }
}
